<?php
/**
 * Created by PhpStorm.
 * User: yuanbo
 * Date: 2018/8/15
 * Time: 下午8:25
 */

namespace app\admin\controller;


use think\Controller;

class Master extends  Controller
{
    //************大师列表********
    public function masterList()
    {
        $masterList = db('master')->join('masterType','master.type_id=mastertype.type_id ')->select();
        $this->assign('masterList',$masterList);
        return $this->fetch('Master/masterList');
    }
    //**********修改大师**********
    public function  updateMaster()
    {
        //大师id
        $master_id = input('master_id');
        $master = db('master')->join('masterType','master.type_id=master_type.type_id')
            ->where('master_id',$master_id)->find();
        $this->assign('master',$master);
        $typeList = db('master_type')->select();
        $this->assign('typeList',$typeList);
        return $this->fetch('Master/updateMaster');
    }
    //修改
    public function update()
    {
        //大师id
        $master_id = input('master_id');
        //大师类型
        $type = input('master_type');
        //大师名称
        $master_name = input('master_name');
        //大师简介
        $master_intro = input('master_intro');
        //大师地址
        $master_address = input('master_address');
        //关注度
        $carevalue = input('carevalue');
        //大师图片
        $master_pic = request()->file('master_pic');

        $updateDate = ['type_id'=>$type,'master_name'=>$master_name,'master_intro'=>$master_intro
            ,'master_address'=>$master_address,'carevalue'=>$carevalue];
        if(!is_null($master_pic))
        {
            $pic = $master_pic->rule('uniqid')->move('../public/static/img/master/uploads/pic');
            if ($pic)
            {
                $updateDate['master_pic']='uploads/pic'.$pic->getFilename();
            }
            else
            {
                //上传失败获取错误信息
                echo $pic->getError();
                $this->error('上传图片失败');
            }
            $rs = db('master')->where('master_id',$master_id)->update($updateDate);
            if ($rs)
            {
                $this->success('修改成功且上传图片成功');
            }
            else {
                $this->error('修改失败');
            }
        }
        else
        {
            $rs = db('master')->where('master_id',$master_id)->update($updateDate);
            if ($rs)
            {
                $this->success('修改成功且上传图片成功');
            }
            else {
                $this->error('修改失败');
            }
        }
    }
  //**********添加大师********
    public function  addMaster()
    {
        $typeList = db('mastertype')->select();
        $this->assign('typeList',$typeList);
        return $this->fetch('Master/addMaster');

    }
    //添加
    public function  add()
    {
        $type = input('master_type');
        //大师名称
        $master_name = input('master_name');
        //大师简介
        $master_intro = input('master_intro');
        //大师地址
        $master_address = input('master_address');
        //关注度
        $carevalue = input('carevalue');
        //大师图片
        $master_pic = request()->file('master_pic');
        $createTime = date("Y:m:d h:i:s");
        $addDate = ['master_id'=>null,'type_id'=>$type,'master_name'=>$master_name,'master_intro'=>$master_intro
            ,'master_address'=>$master_address,'carevalue'=>$carevalue,'create_time'=>$createTime];

        //移动到上传的目录里
        $info = $master_pic->rule('uniqid')->move('../public/static/img/master/uploads/pic');
        if($info)
        {
            $addDate['master_pic']='uploads/pic/' . $info->getFilename();
            $rs = db('master')->insert($addDate);
            if ($rs)
            {
                $this->success('上传图片且添加信息成功');
            }
            else
            {
                $this->error('添加失败');
            }
        }
        else
        {
            //上传失败获取错误信息
            echo $master_pic->getError();
            $this->error('上传图片失败');
        }
    }

}